package com.hailiao.beidou.message.ui.page.search

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.viewmodel.compose.viewModel
import com.d10ng.compose.ui.AppColor
import com.d10ng.compose.ui.PageTransitions
import com.d10ng.compose.ui.base.CellGroup
import com.d10ng.compose.ui.base.CellTitle
import com.d10ng.compose.ui.form.Search
import com.d10ng.compose.ui.navigation.NavBar
import com.hailiao.beidou.message.data.db.entity.ContactEntity
import com.hailiao.beidou.message.data.db.entity.MatchedMessageCount
import com.hailiao.beidou.message.ui.page.destinations.ChatScreenDestination
import com.hailiao.beidou.message.ui.page.destinations.SearchDetailScreenDestination
import com.hailiao.beidou.message.ui.page.search.view.SearchAvatarCell
import com.hailiao.beidou.message.ui.page.search.view.SearchEmpty
import com.hailiao.beidou.message.utils.back
import com.hailiao.beidou.message.utils.go
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootNavGraph

/**
 * 搜索
 * @Author d10ng
 * @Date 2024/3/2 14:35
 */
@RootNavGraph
@Destination(style = PageTransitions::class)
@Composable
fun SearchScreen(
    model: SearchScreenViewModel = viewModel()
) {
    val search by model.searchFlow.collectAsState()
    val contacts by model.contactsFlow.collectAsState()
    val messageCounts by model.messageCountsFlow.collectAsState()

    SearchScreenView(
        search = search,
        contacts = contacts,
        messageCounts = messageCounts,
        onUpdateSearch = model::onUpdateSearch,
        onClickContact = { contact ->
            go(ChatScreenDestination(contact.number).route)
        },
        onClickMessage = { m ->
            go(SearchDetailScreenDestination(m.number, m.name, search).route)
        }
    )
}

@Composable
private fun SearchScreenView(
    search: String = "",
    contacts: List<ContactEntity> = listOf(),
    messageCounts: List<MatchedMessageCount> = listOf(),
    onUpdateSearch: (String) -> Unit = {},
    onClickContact: (ContactEntity) -> Unit = {},
    onClickMessage: (MatchedMessageCount) -> Unit = {}
) {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(AppColor.Neutral.bg)
            .navigationBarsPadding()
    ) {
        NavBar(
            title = "搜索",
            titleAlignment = Alignment.CenterStart,
            border = true,
            onClickBack = { back() })
        CellGroup {
            Search(
                value = search,
                placeholder = "搜索用户名称、卡号、消息",
                onValueChange = onUpdateSearch
            )
        }
        if (contacts.isEmpty() && messageCounts.isEmpty()) {
            SearchEmpty()
        } else {
            LazyColumn(
                modifier = Modifier
                    .fillMaxSize()
                    .weight(1f),
            ) {
                if (contacts.isNotEmpty()) {
                    item {
                        CellTitle(title = "联系人")
                    }
                    items(contacts, { "chat_${it.number}" }) { item ->
                        SearchAvatarCell(
                            name = item.name,
                            des = item.number,
                            onClick = { onClickContact(item) })
                    }
                }
                if (messageCounts.isNotEmpty()) {
                    item {
                        CellTitle(title = "短信记录")
                    }
                    items(messageCounts, { "message_${it.number}" }) { item ->
                        SearchAvatarCell(
                            name = item.name,
                            des = "${item.count}条相关短信",
                            onClick = { onClickMessage(item) })
                    }
                }
            }
        }
    }
}

@Preview
@Composable
private fun SearchScreenPreview() {
    SearchScreenView()
}